/**
 * Created by Suo on 2017/3/28.
 */
client:
window.socketObj.socketIO.emit('joinRoom',socketUserName,socketRoomName,function(backData){
    if (consultationVideoPageControl.ifLog) {
        console.log("joinRoom backData:"+backData.state);
    }
    switch(backData.state){
        case 'error':/*信息错误*/
            consultationVideoPageControl.layerStatus.loadIndex = layer.msg("系统错误....",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
            break;
        case 'ok':/*加入成功*/
            layer.closeAll();
            break;
        case 'alreadyInRoom':/*申请者冲突*/
            consultationVideoPageControl.layerStatus.loadIndex = layer.msg("已在会诊房间或此帐号在别处正在会诊....",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
            break;
        case 'roomFull':/*专家冲突*/
            consultationVideoPageControl.layerStatus.loadIndex = layer.msg("其他 专家正在进行本次会诊,您可稍后再试...",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
            break;
        default:
            break;
    }
});

server:
    socket.on('joinRoom', function (userName,roomName,callback) {
        user = userName;
        roomID = roomName;

        if (roomID && !roomInfo[roomID]) {
            roomInfo[roomID] = [];
        }
        if (!roomID || !user){
            if(callback) {
                callback({'state': "error"});
            }
            return false;
        }
        if (roomInfo[roomID].length >= 2){
            if(callback) {
                callback({'state': "roomFull"});
            }
            return false;
        }
        if(roomInfo[roomID].indexOf(user)!=-1){
            if(callback) {
                callback({'state': "alreadyInRoom"});
            }
            return false;
        }

        if(roomInfo[roomID].length === 1){
            var otherUser = "";
            for (var u in roomInfo[roomID]){
                if (user != roomInfo[roomID][u]){
                    otherUser = roomInfo[roomID][u];
                }
            }
            var commandMsg = {};
            commandMsg.command = 'checkUser';
            commandMsg.otherUser = otherUser;
            socket.emit('command', userName, commandMsg,function (backData) {
                if (ifLog){
                    console.log('checkUser backData :'+backData);
                }
                if (backData === 'ok'){
                    joinRoomFun();
                }else{
                    socket.disconnect();
                }
            });
        }
        if (roomInfo[roomID].length === 0){
            joinRoomFun();
        }

        function joinRoomFun() {
            if (!roomID || !user){
                if(callback) {
                    callback({'state': "error"});
                }
                return false;
            }
            if (roomInfo[roomID].length >= 2){
                if(callback) {
                    callback({'state': "roomFull"});
                }
                return false;
            }
            if(roomInfo[roomID].indexOf(user)!=-1){
                if(callback) {
                    callback({'state': "alreadyInRoom"});
                }
                return false;
            }
            // 将用户昵称加入房间名单中
            roomInfo[roomID].push(user);
            socket.join(roomID);// 加入房间
            joinRoomFlag = true;

            if(ifLog){
                console.log(socket.adapter.rooms[roomID].sockets)
            }
            // 通知房间内人员
            //socketIO.to(roomID).emit('sys', user + '加入了房间', roomInfo[roomID]);
            if(ifLog) {
                console.log("n2===>" + user + '加入了' + roomID + '房间, 人数为:' + roomInfo[roomID].length);
            }
            if(callback) {
                callback({'state':"ok"});
            }
            if(roomInfo[roomID].length === 2){
                //socketIO.emit('sys', userName, 'start');
                var otherUser = "";
                for (var u in roomInfo[roomID]){
                    if (user != roomInfo[roomID][u]){
                        otherUser = roomInfo[roomID][u];
                    }
                }
                var commandMsg = {};
                commandMsg.command = 'start';
                commandMsg.otherUser = otherUser;
                socket.emit('command', userName, commandMsg);
            }
        }
    });

client:
    socket.on('command', function (user, commondMsg,callBack) {
        if(self.ifLog){
            console.log("==================="+user+"======="+socketUserName+"======"+commondMsg.command+"===");
        }

        if(user != socketUserName){
            return;
        }
        if(commondMsg){
            handleContorlCommand : function(comm,callBack){
                if(consultationVideoPageControl.ifLog){
                    console.log("command:"+comm.command);
                }
                switch(comm.command){
                    case "init":
                        consultationVideoPageControl.joinRoom();
                        break;
                    case "start":
                        var expertInfoViewEle = document.getElementById("expertInfoView");
                        expertInfoViewEle.innerText = "正在会诊";

                        consultationVideoPageControl.sendPCCreateOffer();
                        break;
                    case "checkUser":
                        if(callBack){
                            callBack('ok');
                        }
                        break;
                    case "otherSiteOnVideo":
                        consultationVideoPageControl.layerStatus.loadIndex = layer.msg("此帐号在别处正在会诊....",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
                        break;
                    case "remoteLeftRoom":
                        consultationVideoPageControl.layerStatus.loadIndex = layer.msg("对方离开!",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
                        break;
                    default:
                        break;
                }
            }
        }
    });